/*
    Copyright (c) 2009 Pavol Malosek a.k.a. malo

    This file is part of ComComLib.

    ComComLib is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ComComLib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <lcc/api.hpp>

#include <assert.h>
#include <unistd.h>
#include <iostream>

void wait_for_ready (lcc::api_t *api, unsigned int logger_id_)
{
    //  Wait for device to become ready.
    while (true) {
        if (!api->ready (logger_id_))
            sleep (1);
        else 
            break;
    }
}

int main (int argv, char *argc [])
{

    if (argv != 2) {
        std::cerr << "Usage: logger_online ttyname" << std::endl;
        return 1;
    }

    lcc::api_t api;
    
    int room_logger = api.create_logger (lcc::Rxxxx, argc [1]);
    assert (room_logger >= 0);

    //  Define measuring period.
    unsigned int measure_period = 30;

    while (true) {

        //  Check for logger presence.
        api.touch_logger (room_logger);

        wait_for_ready (&api, room_logger);

        if (!api.touch_logger_ok (room_logger)) {
            std::cout << "Could not communicate with logger." << std::endl;
            break;
        }

        std::cout << "Logger " << room_logger << " is alive." << std::endl;

        //  Read logger service memory
        api.read_service_memory (room_logger);

        wait_for_ready (&api, room_logger);

        if (!api.read_service_memory_ok (room_logger)) {
            std::cout << "Could not read service memory." << std::endl;
            break;
        }

        std::cout << "Service memory read from logger " << 
            room_logger << " ok." << std::endl;

        lcc::device_info_t info;

        if (!api.get_device_info (room_logger, &info)) {
            std::cout << "Could not fill logger info." << std::endl;
            break;
        }

        lcc::print_device_info (&info);

        //  Start online measure.
        api.start_online_measure (room_logger, measure_period / 2);
        
        wait_for_ready (&api, room_logger);

        if (!api.start_online_measure_ok (room_logger)) {
            std::cout << "Could not start online measure." << std::endl;
            break;
        }

        sleep (1);

        uint64_t last_stamp = 0;

        while (true) {

            lcc::record_t record = api.get_online_record (room_logger);

            if (last_stamp != record.stamp) {
                print_record (&record);
                last_stamp = record.stamp;
            } else {
                std::cerr << "!!!!!LOGER FAILURE!!!!!" << std::endl;
            }

            sleep (measure_period);
        }

        break;
    }

    api.destroy_logger (room_logger);

    return 0;
}
